package com.chat.utils;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
public class FileUploadUtils {
	private static OSSClient client;
	private static String bucket_name		= PropertiesUtil.getParm("oss.bucket_name");
	private static String endpoint			= PropertiesUtil.getParm("oss.endpoint");
	private static String accessKeyId		= PropertiesUtil.getParm("oss.accessKeyId");
	private static String accessKeySecret	= PropertiesUtil.getParm("oss.accessKeySecret");
	public static ClientConfiguration conf() {
		ClientConfiguration conf = new ClientConfiguration();
		conf.setMaxConnections(200);
		conf.setSocketTimeout(10000);
		conf.setMaxErrorRetry(3);
		return conf;
	}
	public static boolean open() {
		if (client == null) {
			client = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf());
		}
		return true;
	}
	public static boolean close() {
		if (client != null) {
			client.shutdown();
		}
		return true;
	}
	public static void listObjects() {
		ObjectListing objectListing = client.listObjects(bucket_name);
		for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
		}
	}
	public static void makeUrl(String key) {
		try {
			GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket_name, key, HttpMethod.GET);
			Date expiration = new Date(new Date().getTime() + 3600 * 1000);
			request.setExpiration(expiration);
			URL signedUrl = client.generatePresignedUrl(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@SuppressWarnings("unused")
    public static void upload(String filePath, String key) {
		try {
			ObjectMetadata meta = new ObjectMetadata();
			meta.setContentType("text/html");
			File file = new File(filePath);
			PutObjectResult rs = client.putObject(bucket_name, key, file, meta); 
			if(file.delete()) {
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@SuppressWarnings("unused")
    public static void uploadImage(String filePath, String key) {
		try {
			ObjectMetadata meta = new ObjectMetadata();
			meta.setContentType("image/jpeg");
			PutObjectResult rs = client.putObject(bucket_name, key, new File(filePath), meta);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void OssUpload(byte[] content, String key) {
		try {
			client.putObject(bucket_name, key, new ByteArrayInputStream(content));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void download(String srcPath, String destPath) {
		try {
			client.getObject(new GetObjectRequest(bucket_name, srcPath), new File(destPath));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public InputStream ossDownload() {
		OSSObject ossObject = client.getObject(bucket_name, "UAT/upload_test_001.xml");
		InputStream is = new DataInputStream(ossObject.getObjectContent());
		return is;
	}
	public void delete(String name) {
		client.deleteObject(bucket_name, name);
	}
}